from django.contrib.auth.models import Permission, Group
from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAdminUser
from django.contrib.contenttypes.models import ContentType
from rest_framework.response import Response
from rest_framework.decorators import action

from meiduo_admin.serializers.permission import PermissionSerializer,ContentTypeSerializer,GroupSerializer, PermissionSimpleSerializer, AdminSerializer, GroupSimpleSerializer
from users.models import User


class PermissionViewSet(ModelViewSet):
    # 第难以用户管理视图集
    permission_classes = [IsAdminUser]

    # 指定序列化器类
    serializer_class = PermissionSerializer
    # 指定查询集
    queryset = Permission.objects.all()

    # GET /meiduo_admin/permission/admins/ -> list
    # POST /meiduo_admin/permission/admins/ -> create
    # GET /meiduo_admin/permission/admins/(?P<pk>\d+)/ -> retrieve
    # PUT /meiduo_admin/permission/admins/(?P<pk>\d+)/ -> update
    # DELETE /meiduo_admin/permission/admins/(?P<pk>\d+)/ -> destroy

    # def list(self, request):
    #     qs = self.get_queryset()
    #     serializer = self.get_serializer(qs, many=True)
    #     return Response(serializer.data)

    # def create(self, request, *args, **kwargs):
    #     serializer = self.get_serializer(data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #
    #     serializer.save() # -> create
    #     return Response(serializer.data, status=status.HTTP_201_CREATED)

    # def retrieve(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer = self.get_serializer(instance)
    #     return Response(serializer.data)

    # def update(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     serializer = self.get_serializer(instance, data=request.data)
    #     serializer.is_valid(raise_exception=True)
    #     serializer.save() # update
    #     return Response(serializer.data)

    # def destroy(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     instance.delete()
    #     return Response(status=status.HTTP_204_NO_CONTENT)

    # GET `/meiduo_admin/permission/content_types/`
    def Content_types(self, request):

        content_types = ContentType.objects.all()

        serializer = ContentTypeSerializer(content_types,many=True)

        return Response(serializer.data)



class GroupViewSet(ModelViewSet):
    # 指定用户组视图集

    permission_classes = [IsAdminUser]

    serializer_class = GroupSerializer

    queryset = Group.objects.all()

    # GET `/meiduo_admin/permission/simple/
    def Simple(self, request):
        """获取权限数据"""
        # 1. 获取权限数据
        permissions = Permission.objects.all()

        # 2. 将权限数据序列化并返回
        serializer = PermissionSimpleSerializer(permissions, many=True)
        return Response(serializer.data)


class AdminViewSet(ModelViewSet):
    # 用户管理员视图集
    permission_classes = [IsAdminUser]
    # 指定序列化器类
    serializer_class = AdminSerializer
    # 指定查询集
    queryset = User.objects.filter(is_staff=True)

    # GET /meiduo_admin/permission/groups/simple/
    def Simple(self, request):
        # 用户分组简单展示

        # 1. 获取用户组数据
        groups = Group.objects.all()

        # 2. 将用户组数据序列化并返回
        serializer = GroupSimpleSerializer(groups, many=True)
        return Response(serializer.data)

